﻿using System;
using System.Xml;

namespace Graphe.Collations
{
    public class Fragment : ICollationElement, IComparable<Fragment>
    {
        #region Public Properties

        public string Text { get; set; }
        public string Manuscript { get; set; }

        #endregion
        #region ICollationElement Members

        public void Parse(XmlReader reader, CollationDebugMessages debugMessages)
        {
            if (reader.MoveToFirstAttribute())
            {
                do
                {
                    if (reader.Name == "Manuscript")
                    {
                        Manuscript = reader.ReadContentAsString();
                    }
                    else
                    {
                        debugMessages.Add(String.Format("Unknown attribute fragment xml: {0}.", reader.Name));
                    }

                } while (reader.MoveToNextAttribute());
            }

            reader.ReadToFollowing("Text");
            reader.Read();
            this.Text = reader.ReadContentAsString();
            reader.Read();
        }

        public void Render(XmlTextWriter writer)
        {
            writer.WriteStartElement("Fragment");

            writer.WriteAttributeString("Manuscript", Manuscript);

            {
                writer.WriteStartElement("Text");
                writer.WriteString(Text);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }

        #endregion
        #region IComparable<Fragment> Members

        public int CompareTo(Fragment other)
        {
            if (Manuscript != other.Manuscript || Text != other.Text)
                return -1;
            else
                return 0;
        }

        #endregion
    }
}
